package com.project.controller;


import com.Entity.DzmHisInspectionfeeEntity;
import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.config.annotation.Reference;
import gz.NewDoctorService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(value = {"*"},allowCredentials = "true")
public class NewDoctorController {

    @Reference
    private NewDoctorService ns;


    /**
     * 点击选择患者按钮，查询所有患者
     * @param name
     * @return
     */
    @RequestMapping("nw/queryPatient")
    public  List<Map<String,Object>> all(String name){
        System.out.println("传过来的参数："+name);
        List<Map<String,Object>> all = ns.findAll(name);
        return all;
    }


    /**
     *根据返回的患者id查询某位患者的信息（历史病历和患者档案）
     * @param request
     * @return
     */
    @RequestMapping("getOnePatient")
    public  Map<String,Object> one(HttpServletRequest request){
        String id = request.getParameter("patient_id");
        System.out.println("patient_id:"+id);
        Integer patient_id = Integer.valueOf(id);
        List<Map<String,Object>> all = ns.getOnePatient(patient_id);
        System.out.println("All是否为空："+all.isEmpty());
        List<Map<String,Object>> fileAll = ns.getPatientFile(patient_id);
        System.out.println("fileAll是否为空："+fileAll.isEmpty());
        List<Map<String,Object>> history = ns.getHistoryFile(patient_id);
        System.out.println("history是否为空："+history);
        Map<String,Object> map = new HashMap<>();
        map.put("patient",all);
        map.put("patientfile",fileAll);
        map.put("historys",history);
        return map;
    }


    /**
     *查询药品信息
     * @param medicines_name
     * @return
     */
    @RequestMapping("ChineseMedicines")
    public List<Map<String,Object>> ChinaMedicines(String medicines_name){
        System.out.println("药品名称：" +medicines_name);
        List<Map<String,Object>> all = ns.ChineseMedicines(medicines_name);
        System.out.println("查询的数据："+all);
        return all;
    }

    /**
     * 查询检查项目信息
     * @param inspection_name
     * @return
     */
    @RequestMapping("Check_Project")
    public List<Map<String, Object>> Check_project(String inspection_name){
        System.out.println("项目名称：" +inspection_name);
        List<Map<String, Object>> all = ns.check_project(inspection_name);
        System.out.println("查询的数据："+all);
        return all;
    }


    /**
     * 查询附加费用信息
     * @return
     */
   @RequestMapping("add_cost")
    public   List<Map<String, Object>> cost(){
       List<Map<String, Object>> cost =   ns.additional_cost();
       System.out.println("附加费用："+cost);
        return cost;
   }

    /**
     *库存预警
     * @return
     */
   @RequestMapping("queryMedicines")
    public List<Map<String, Object>> all(){
       List<Map<String, Object>> all = ns.all_midecines();
        return all;
   }


    /**
     * 查询出医生选择的药品（药品、检查项目和附加费用）添加到处方里面
     * @param request
     * @return
     */
   @RequestMapping("selectCost")
    public  Map<String,Object> select(HttpServletRequest request){
        String mid = request.getParameter("medicines_id");
        String pid = request.getParameter("pre_id");
        String id = request.getParameter("ins_id");
        System.out.println("mid:"+mid+"***+pid:"+pid+"***id:"+id);
        List<String> list = new ArrayList<>();
        list.add(mid);
        List<String> list1 = new ArrayList<>();
        list1.add(pid);
        List<String> list2 = new ArrayList<>();
        list2.add(id);
        List<Map<String, Object>> all = new ArrayList<>();
        if(mid!=null&&mid.length()>0){
            List<Map<String, Object>> all1 = ns.select_mid(list);
            System.out.println("数据:"+all1);
            for (int i = 0;i<all1.size();i++){
                all.add(all1.get(i));
            }
        }
        if(pid!=null&&pid.length()>0) {
            List<Map<String, Object>> all2 = ns.select_pid(list1);
            System.out.println("数据:"+all2);
            for (int i = 0;i<all2.size();i++){
                all.add(all2.get(i));
            }
        }
        if(id!=null&&id.length()>0){
            List<Map<String, Object>> all3 = ns.select_id(list2);
            System.out.println("数据:"+all3);
            for (int i = 0;i<all3.size();i++){
                all.add(all3.get(i));
            }
        }
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("data",all);
        return map;
   }


    /**
     * 点击保存按钮将该患者的所有信息添加相关表格中，将该患者的支付状态该为未支付，就诊状态改为已就诊
     * @param map
     * @param request
     * @return
     */
   @RequestMapping("shouFei")
    public Map<String,Object> shoufei(@RequestParam Map<String,Object> map,HttpServletRequest request){
       String case_code = request.getParameter("case_code");
       String id = request.getParameter("patient_id");
       Integer patient_id = Integer.valueOf(id);
       System.out.println("case_code:"+case_code);
       System.out.println("返回的集合："+map);
       int ii = ns.update_regisid(patient_id);
       int i = ns.insert_history(map);
       int s = ns.insert_order(map);
       int code = ns.select_historyId(case_code);
       System.out.println("historyid:"+code);
       int a = ns.update_orderId(code,case_code);
       System.out.println(a+i+s+ii+"行受影响");
       Map<String,Object> maps = new HashMap<>();
       if(i>0&&s>0&&a>0&&ii>0){
           maps.put("msg","true");
       }else{
           maps.put("msg","false");
       }
        return maps;
   }

}
